package com.woniuxy.venueservice.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.status.VenueState;
import com.woniuxy.core.util.DateUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.venueclient.client.AdminFeignClient;
import com.woniuxy.venueclient.dto.AdminDto;
import com.woniuxy.venueclient.form.AdminByIdForm;
import com.woniuxy.venuedao.mapper.VenueApplyMapper;
import com.woniuxy.venuedao.mapper.VenueInfoMapper;
import com.woniuxy.venuedao.model.VenueApply;
import com.woniuxy.venuedao.model.VenueInfo;
import com.woniuxy.venuemodel.dto.VenueApplyDto;
import com.woniuxy.venuemodel.form.RefuseVenueApplyForm;
import com.woniuxy.venuemodel.param.AuthVenueApplyParam;
import com.woniuxy.venuemodel.param.FindVenueApplyParam;
import com.woniuxy.venueservice.exception.VenueException;
import com.woniuxy.venueservice.service.VenueApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 场馆申请入驻表 服务实现类
 * </p>
 *
 * @author 冉杰
 * @since 2024-01-15
 */
@Service
public class VenueApplyServiceImpl extends ServiceImpl<VenueApplyMapper, VenueApply> implements VenueApplyService {

    @Autowired
    private VenueApplyMapper venueApplyMapper;
    @Autowired
    private VenueInfoMapper venueInfoMapper;
    @Resource
    private AdminFeignClient adminFeignClient;
    @Override
    public PageInfo<VenueApply> findVenueApplyByCondition(FindVenueApplyParam param) {
        QueryWrapper<VenueApply> wrapper = new QueryWrapper<>();
        wrapper.like(!ObjectUtil.isEmpty(param.getVenueName()),"venue_name",param.getVenueName());
        wrapper.like(!ObjectUtil.isEmpty(param.getVenuePhone()),"venue_phone",param.getVenuePhone());
        wrapper.eq(!ObjectUtil.isEmpty(param.getAuthState()),"auth_state",param.getAuthState());
        Page<VenueApply> page = new Page<>(param.getPageNum(),param.getPageSize());
        page(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(page.getRecords());
        return pageInfo;
    }

    @Override
    public void authVenue(AuthVenueApplyParam param) {
        VenueApply venueApply = venueApplyMapper.selectById(param.getApplyId());
        if(BeanUtil.isEmpty(venueApply)){
            throw new VenueException(5001,"申请不存在");
        }
        if(venueApply.getAuthState()!=0){
            throw new VenueException(5002,"该场馆申请认证状态异常");
        }
        QueryWrapper<VenueInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("venue_phone",venueApply.getVenuePhone());
        VenueInfo venueInfo1 = venueInfoMapper.selectOne(wrapper1);
        if(ObjectUtil.isEmpty(venueInfo1.getVenueName())
                ||ObjectUtil.isEmpty(venueInfo1.getVenuePhone())
                ||ObjectUtil.isEmpty(venueInfo1.getVenuePass())
                ||ObjectUtil.isEmpty(venueInfo1.getVenueAccount())
                ||ObjectUtil.isEmpty(venueInfo1.getVenueImg())
                ||ObjectUtil.isEmpty(venueInfo1.getVenueLicense())
                ||ObjectUtil.isEmpty(venueInfo1.getVenueRemark())) throw new VenueException(5003,"场馆信息未完善,不能认证");
        //修改申请表数据
        venueApply.setAuthState(VenueState.IS_AUTH.getState());
        AdminByIdForm adminByIdForm = new AdminByIdForm();
        adminByIdForm.setAdminId(param.getAdminId());
        Result<AdminDto> result = adminFeignClient.byId(adminByIdForm);
        AdminDto adminDto = result.getData();
        venueApply.setAdminName(adminDto.getAdminName());
        venueApply.setDoTime(DateUtil.dateTimeToString(new Date()));
        venueApplyMapper.updateById(venueApply);
        //修改场馆表认证状态
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_phone",venueApply.getVenuePhone());
        VenueInfo venueInfo = venueInfoMapper.selectOne(wrapper);
        venueInfo.setAuthState(VenueState.IS_AUTH.getState());
        venueInfoMapper.updateById(venueInfo);
    }

    @Override
    public void refuseVenue(RefuseVenueApplyForm form, Integer adminId) {
        VenueApply venueApply = venueApplyMapper.selectById(form.getApplyId());
        if(BeanUtil.isEmpty(venueApply)){
            throw new VenueException(5001,"申请不存在");
        }
        if(venueApply.getAuthState()!=0){
            throw new VenueException(5002,"该场馆申请认证状态异常");
        }
        //修改申请表数据
        venueApply.setAuthState(2);
        venueApply.setMessage(form.getMessage());
        AdminByIdForm adminByIdForm = new AdminByIdForm();
        adminByIdForm.setAdminId(adminId);
        Result<AdminDto> result = adminFeignClient.byId(adminByIdForm);
        AdminDto adminDto = result.getData();
        venueApply.setAdminName(adminDto.getAdminName());
        venueApply.setDoTime(DateUtil.dateTimeToString(new Date()));
        venueApplyMapper.updateById(venueApply);
        //拒绝后删除场馆表中的信息
        QueryWrapper<VenueInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_phone",venueApply.getVenuePhone());
        venueInfoMapper.delete(wrapper);
    }

    @Override
    public List<VenueApplyDto> searchVenueByPhone(String phone) {
        QueryWrapper<VenueApply> wrapper = new QueryWrapper<>();
        wrapper.eq("venue_phone",phone);
        List<VenueApply> list = venueApplyMapper.selectList(wrapper);
        List<VenueApplyDto> dtos = BeanUtil.copyToList(list, VenueApplyDto.class);
        return dtos;
    }
}
